<template>
  <el-card>
    <el-form inline>
      <el-form-item>
        <el-input placeholder="角色名称" v-model="roleName" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search">查询</el-button>
        <el-button>清空</el-button>
      </el-form-item>
    </el-form>

    <el-button type="primary" @click="showRole">添加</el-button>
    <el-button type="danger">批量删除</el-button>

    <el-table :data="roleList" border class="role-table">
      <el-table-column type="selection" width="60" />
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column label="角色名称" prop="roleName" />
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button @click="goRoleAuth(row)" size="small" type="primary" :icon="User" />
          <el-button size="small" type="warning" :icon="Edit"  v-permission="'btn.Role.update'"/>
          <el-button size="small" type="danger" :icon="Delete" v-permission="'btn.Role.remove'"/>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:currentPage="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5, 10, 15, 20]"
      :small="true"
      layout="prev, pager, next, jumper, ->, sizes, total"
      :total="total"
      @size-change="getRoleList"
      @current-change="getRoleList"
    />
  </el-card>

  <el-dialog v-model="visible" title="添加角色">
      <el-form label-width="100px" ref="roleFormRef" :model="roleFormData" :rules="roleFormRules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="roleFormData.roleName" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="saveRole">确定</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script lang="ts">
export default {
  name: "role",
};
</script>

<script lang="ts" setup>

import { ref, onMounted, reactive } from "vue";
import { Search, User, Edit, Delete } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import { useRouter } from "vue-router";
import {getRoleListApi,saveRoleApi} from "@/api/acl/role"
import type {allRolesList} from "@/api/acl/model/userModel"
const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);

const roleName=ref("")
const roleList=ref<allRolesList>([])

const visible=ref(false)

const router=useRouter()
//显示分配权限组件
const goRoleAuth=(row:any)=>{
  router.push({
    name: "RoleAuth",
    query:{
      id:row.id,
      roleName:row.roleName
    }
  })
}

const roleFormData=reactive<any>({
  roleName:""
})

const showRole=()=>{
  visible.value=true
  roleFormData.roleName=""
}

const getRoleList=async()=>{
const res=await getRoleListApi({
 page: currentPage.value,
 limit: pageSize.value, 
  roleName: roleName.value,
 
})
// console.log(res)
total.value=res.total as number
roleList.value=res.items
}
onMounted(getRoleList)

const roleFormRef=ref()
const roleFormRules = reactive<FormRules>({
  roleName: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
});

const saveRole=async()=>{
  await  roleFormRef.value?.validate()
const res= await saveRoleApi(roleFormData.roleName)
visible.value=false
ElMessage.success("添加角色成功");
getRoleList()
}


</script>

<style scoped></style>
